package com.example.joysinfo.lkc.receiver;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.example.joysinfo.lkc.MainActivity;
import com.example.joysinfo.lkc.bean.Tuisongbean;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by cuiyuhu on 16-5-10.
 */
public class MessageReceiver extends PushMessageReceiver {
    private String mRegId;
    private long mResultCode = -1;
    private String mReason;
    private String mCommand;
    private String mMessage;
    private String mTopic;
    private String mAlias;
    private String mAccount;
    private String mStartTime;
    private String mEndTime;
    private String TAG="MESSAGE";
    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage message){
        Log.v(TAG,
                "onReceivePassThroughMessage is called. " + message.toString());

        Log.d("xing","xiaoxi1"+message.toString());

        if(!TextUtils.isEmpty(message.getTopic())) {
            mTopic=message.getTopic();
        } else if(!TextUtils.isEmpty(message.getAlias())) {
            mAlias=message.getAlias();
        }
//        Intent intent=new Intent();
//        intent.setAction(MainActivity.RECEIVER_ACTION);
//        intent.putExtra("message",message.getContent());
//        context.sendBroadcast(intent);
    }

    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage message){
        Log.v(TAG,
                "onNotificationMessageClicked is called. " + message.toString());
        Log.d("tuisonghuilai", "xiaoxi2" + message.getContent().toString());

        String eid = message.getExtra().get("msgID");
        Log.d("tuisonghuilai",message.toString()+"    "+eid);
        Intent intent=new Intent(context.getApplicationContext(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("msgID", eid);
        context.getApplicationContext().startActivity(intent);

    }

    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage message){
        Log.v(TAG,
                "onNotificationMessageArrived is called. " + message.toString());
        Log.d("xing", "xiaoxi3" + message.getExtra().get("eid"));

    }

    @Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {
        Log.v(TAG,
                "onCommandResult is called. " + message.toString());
        Log.d("xing", "xiaoxi4" + message.toString());
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                String mRegID = arguments.get(0);
                Log.d("xing", "mRegID" + mRegID);
                MainActivity.xiaomiregid=mRegID;
                MainActivity.sendId(context);
            }
        }

    }

    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage message){
        Log.v(TAG,
                "onReceiveRegisterResult is called. " + message.toString());
        Log.d("xing", "xiaoxi5" + message.toString());
        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String log;

    }

    @SuppressLint("SimpleDateFormat")
    public static String getSimpleDate() {
        return new SimpleDateFormat("MM-dd hh:mm:ss").format(new Date());
    }




}
